package com.yuanqi.repository;

import com.yuanqi.domain.PaymentRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PaymentRecordRepository extends JpaRepository<PaymentRecord, Integer> {
    
    Optional<PaymentRecord> findByPaymentNumber(String paymentNumber);
    
    List<PaymentRecord> findByOrderNumber(String orderNumber);
    
    List<PaymentRecord> findByStatus(Integer status);
    
    @Query("SELECT p FROM PaymentRecord p WHERE " +
           "(:paymentNumber IS NULL OR p.paymentNumber LIKE %:paymentNumber%) AND " +
           "(:orderNumber IS NULL OR p.orderNumber LIKE %:orderNumber%)")
    List<PaymentRecord> findByConditions(@Param("paymentNumber") String paymentNumber,
                                        @Param("orderNumber") String orderNumber);
    
    @Query("SELECT COUNT(p) FROM PaymentRecord p WHERE p.status = :status")
    long countByStatus(@Param("status") Integer status);
}
